package com.seari.c3gallery.main;

import com.seari.c3gallery.client.UDPClient;
import com.seari.commonutils.common.MiscTools;
import com.seari.commonutils.redis.JedisUtilProxy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.Properties;

@Component
@Order(value = 1)
public class StartUpClass implements ApplicationRunner {

    protected static Logger logger = LoggerFactory.getLogger(StartUpClass.class);

    public static Properties config;
    static String configFilePath = "config.cfg";
    public static UDPClient client = null;
    String remoteHost;
    int remoteport;
    int localPort;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        logger.info("start up class loading...");
        JedisUtilProxy.initJedisPoolUtil("127.0.0.1", 6379);
        init();
    }

    public void init()
    {
        try
        {
            config = MiscTools.loadConfig(configFilePath);
            remoteHost = config.getProperty("remoteHost");
            remoteport = Integer.valueOf(config.getProperty("remoteport"));
            localPort = Integer.valueOf(config.getProperty("localPort"));
            client = UDPClient.getInstance(remoteHost,remoteport,localPort);
            client.setUp();

        } catch (Exception e)
        {
            // TODO: handle exception
            logger.warn(e.getMessage());
        }
        logger.info("read config file complete");
    }

}
